package com.example.prison.mapper;

import com.example.prison.entity.SystemInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author jiang
* @description 针对表【system_info】的数据库操作Mapper
* @createDate 2024-01-17 15:47:25
* @Entity com.example.prison.entity.SystemInfo
*/
@Mapper
public interface SystemInfoMapper extends BaseMapper<SystemInfo> {

    @Select("select * from system_info order by id desc")
    List<SystemInfo> get();

    @Insert("insert into system_info(refresh_time,gateway_address,gateway_port,pulse_solve,pulse_time,pulse_current,high_rated_voltage,rated_voltage,rated_current,defense) " +
            "values (#{refreshTime},#{gatewayAddress},#{gatewayPort},#{pulseSolve},#{pulseTime},#{pulseCurrent},#{highRatedVoltage},#{ratedVoltage},#{ratedCurrent},#{defense})")
    int add(SystemInfo record);

    @Delete("<script>delete from system_info where id in <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item} </foreach> </script>")
    void delete(@Param("ids") Integer[] ids);

    @Delete("delete from system_info")
    void deleteAll();

//    @Update("update system_info set refresh_time=#{info.refreshTime},gateway_address=#{info.gatewayAddress},gateway_port=#{info.gatewayPort}," +
//            "pulse_solve=#{info.pulseSolve},pulse_time=#{info.pulseTime},pulse_current=#{info.pulseCurrent},high_rated_voltage=#{info.highRatedVoltage}," +
//            "rated_voltage=#{info.ratedVoltage},rated_current=#{info.ratedCurrent} ")
    @Update({"<script> UPDATE system_info",
            "<set>",
            "<if test='info.refreshTime != null'>refresh_time = #{info.refreshTime},</if>",
            "<if test='info.gatewayAddress != null'>gateway_address = #{info.gatewayAddress},</if>",
            "<if test='info.gatewayPort != null'>gateway_port = #{info.gatewayPort},</if>",
            "<if test='info.pulseSolve != null'>pulse_solve = #{info.pulseSolve},</if>",
            "<if test='info.pulseTime != null'>pulse_time = #{info.pulseTime},</if>",
            "<if test='info.pulseCurrent != null'>pulse_current = #{info.pulseCurrent},</if>",
            "<if test='info.highRatedVoltage != null'>high_rated_voltage = #{info.highRatedVoltage},</if>",
            "<if test='info.ratedVoltage != null'>rated_voltage = #{info.ratedVoltage},</if>",
            "<if test='info.ratedCurrent != null'>rated_current = #{info.ratedCurrent},</if>",
            "</set>",
            "</script>"})
    int update(SystemInfo info, Integer id);

    @Update("update system_info set gateway_address=#{ip},gateway_port=#{port}")
    int updateIPAndPort(@Param("ip")String ip, @Param("port")Integer port);

    @Select("select * from system_info order by id desc LIMIT 1 ")
    SystemInfo one();

}




